package com.wbao.dianniu.update;

import android.content.Context;
import android.util.Log;

import com.alibaba.mobileim.channel.IMChannel;
import com.alibaba.wxlib.util.SysUtil;
import com.mob.MobSDK;
import com.wbao.dianniu.R;
import com.wbao.dianniu.logical.Const;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.logical.GlobalUserData;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.onekeyshare.ShareContentCustomizeCallback;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;


public class ShareSDKManager {

    private static ShareSDKManager instance;
    private final String APP_KEY = "1475bc40e11d8";
    private final String APP_SECRETS = "0400de1ade4700e112b21b92e24b9c40";
    public static String SERVICE_URL = "http://www.dianniu.me/share/";
    public static String TEST_SERVICE_URL = "http://www.dianniu.me/debugshare/";
//    private static String APP_SHARE_URL = "http://www.dianniu.me/dianniu/share/";

    private ShareSDKManager() {
    }

    public static ShareSDKManager getInstance() {
        if (null == instance)
            instance = new ShareSDKManager();
        return instance;
    }

    /**
     * @param context
     * @param content  分享内容
     * @param type     1： 问答分享 2：名片分享 3：app分享 ,4：求职分享 5： 招聘分享 6：活动分享，7：匿名分享
     * @param imageUrl 图片：type=1 有问答图片显示图片，没有图片显示头像
     *                 type=2 显示头像
     * @param id       问题id，用户Id，求职id，招聘id，活动id
     */
    public void showShare(Context context, String content, int type, String imageUrl, int id) {
//		ShareSDK.initSDK(context);
        MobSDK.init(context, APP_KEY, APP_SECRETS);
        OnekeyShare oks = new OnekeyShare();
        oks.disableSSOWhenAuthorize();
        String url;

        GlobalUserData userData = new GlobalUserData();
        if (userData.getEnvironemt().equals("qa")) {
            url = TEST_SERVICE_URL;
        } else {
            url = SERVICE_URL;
        }
        String end = "&&t=" + userData.getEnvironemt();

        if (1 == type) {
            oks.setTitle("来自电纽的问答分享");
            oks.setImageUrl(imageUrl);
            url += "quest.html?id=" + id + end;
        } else if (2 == type) {
            oks.setTitle("来自电纽的名片分享");
            oks.setImageUrl(imageUrl);
            url += "card.html?id=" + id + end;
        } else if (3 == type) {
            oks.setTitle(context.getResources().getString(R.string.app_share_title));
            oks.setImageUrl(imageUrl);
            url = url + "invite.html?u=" + GlobalContext.getAccountId() + end;
        } else if (4 == type) {
            oks.setTitle("来自电纽的职位分享");
            oks.setImageUrl(imageUrl);
            url += "job.html?id=" + id + end;
        } else if (5 == type) {
            oks.setTitle("来自电纽的招聘分享");
            oks.setImageUrl(imageUrl);
            url += "recruit.html?id=" + id + end;
        } else if (6 == type) {
            oks.setTitle("来自电纽的活动分享");
            oks.setImageUrl(imageUrl);
            url += "activity.html?id=" + id + end;
        } else if (7 == type) {
            oks.setTitle("来自电纽的心情分享");
            oks.setImageUrl(imageUrl);
            url += "aliasQuest.html?id=" + id + end;
        } else if (8 == type) {
            oks.setTitle("来自电纽的干货分享");
            oks.setImageUrl(imageUrl);
            url += "knowledge.html?id=" + id + end;
        }
        oks.setTitleUrl(url);
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl(url);
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl(url);
        oks.setText(content);
//		oks.setComment("我正在使用电纽App，大家赶快加入吧！");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(context.getResources().getString(R.string.app_name));
//		oks.setCallback(listener);
        // 启动分享GUI
        oks.show(context);

    }


    /**
     * 分享到QQ好友
     */
    public void showShareToQQ(Context context, String content, String imageUrl, int id) {

        String url;
        GlobalUserData userData = new GlobalUserData();
        if (userData.getEnvironemt().equals("qa")) {
            url = TEST_SERVICE_URL;
        } else {
            url = SERVICE_URL;
        }
        String end = "&&t=" + userData.getEnvironemt();

        QQ.ShareParams sp = new QQ.ShareParams();
        sp.setTitle("电商交流，我在电纽等你！");
        url = url + "card.html?id=" + id + end;
        sp.setTitleUrl(url);
        sp.setImageUrl(imageUrl);
        sp.setText(content);
        Platform platform = ShareSDK.getPlatform(QQ.NAME);
        platform.setPlatformActionListener(null); // 设置分享事件回调
        // 执行图文分享
        platform.share(sp);
    }

    public void showShareToWechat(Context context, String content, String imageUrl, int id) {
        String url;
        GlobalUserData userData = new GlobalUserData();
        if (userData.getEnvironemt().equals("qa")) {
            url = TEST_SERVICE_URL;
        } else {
            url = SERVICE_URL;
        }
        String end = "&&t=" + userData.getEnvironemt();


        Wechat.ShareParams sp = new Wechat.ShareParams();
        sp.setShareType(Platform.SHARE_WEBPAGE);
        sp.setTitle("电商交流，我在电纽等你！");
        sp.setText(content);
        sp.setImagePath(imageUrl);
        url = url + "card.html?id=" + id + end;
        // url仅在微信（包括好友和朋友圈）中使用
        sp.setUrl(url);
        Platform platform = ShareSDK.getPlatform(Wechat.NAME);
        platform.setPlatformActionListener(null); // 设置分享事件回调
        // 执行图文分享
        platform.share(sp);
    }

    public void showShareToBook() {

    }

    public void shareApp(Context context, int accountId) {
        String url;
        GlobalUserData userData = new GlobalUserData();
        if (userData.getEnvironemt().equals("qa")) {
            url = TEST_SERVICE_URL;
        } else {
            url = SERVICE_URL;
        }
        String end = "&&t=" + userData.getEnvironemt();


        MobSDK.init(context, APP_KEY, APP_SECRETS);
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();
        oks.setTitle(context.getResources().getString(R.string.app_share_title));
        // text是分享文本，所有平台都需要这个字段
        oks.setText(context.getResources().getString(R.string.app_share_content));
        url = url + "invite.html?u=" + accountId + end;
        // titleUrl是标题的网络链接，QQ和QQ空间等使用
        oks.setTitleUrl(url);
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl(url);
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl(url);
        oks.setSilent(true);
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite("电纽");
        oks.setImageUrl(GlobalContext.cdndownUrl + "logo.png");
        oks.show(context);

    }


    public void sharePic(Context context, final String url, final String prefix_url, final String end) {
//        String prefix_url = null;
//        GlobalUserData userData = new GlobalUserData();
//        if (userData.getEnvironemt().equals("qa")) {
//            prefix_url = TEST_SERVICE_URL;
//        } else {
//            prefix_url = SERVICE_URL;
//        }
//        String end = "&&t=" + userData.getEnvironemt();


        MobSDK.init(context, APP_KEY, APP_SECRETS);
        OnekeyShare oks = new OnekeyShare();

        oks.setShareContentCustomizeCallback(new ShareContentCustomizeCallback() {
            @Override
            public void onShare(Platform platform, Platform.ShareParams paramsToShare) {
                if (platform.getName() == QQ.NAME) {
                    paramsToShare.setShareType(Platform.SHARE_IMAGE);
                    paramsToShare.setImageUrl(url);
                    paramsToShare.setImagePath(url);
                    platform.share(paramsToShare);
                } else if (platform.getName() == QZone.NAME) {
                    paramsToShare.setShareType(Platform.SHARE_IMAGE);
                    paramsToShare.setTitle("qq空间分享");
                    paramsToShare.setTitleUrl(prefix_url + "invite.html?u=" + GlobalContext.getAccountId() + end);
                    paramsToShare.setSite("dianniu");
                    paramsToShare.setSiteUrl(prefix_url + "invite.html?u=" + GlobalContext.getAccountId() + end);
                    paramsToShare.setImagePath(url);
                    platform.share(paramsToShare);
                } else if (platform.getName() == Wechat.NAME || platform.getName() == WechatMoments.NAME) {
                    paramsToShare.setShareType(Platform.SHARE_IMAGE);
                    paramsToShare.setImageUrl(url);
                    paramsToShare.setImagePath(url);
                    platform.share(paramsToShare);
                }
            }
        });

        oks.show(context);
    }


    public void shareQQPic(Context context, String url) {
        QQ.ShareParams sp = new QQ.ShareParams();
        sp.setShareType(Platform.SHARE_IMAGE);
        sp.setImageUrl(url);
        sp.setImagePath(url);
        Platform platform = ShareSDK.getPlatform(QQ.NAME);
        platform.setPlatformActionListener(null); // 设置分享事件回调
        // 执行图文分享
        platform.share(sp);
    }


}
